package MyDaily.Servlet.page;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import MyDaily.dao.daily.QuertDaily;
import MyDaily.pojo.Daily;
import MyDaily.pojo.Page;
import MyDaily.pojo.PageUtils;

/**
 * Servlet implementation class PageServlet
 */
@WebServlet("/PageServlet")
public class PageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public PageServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置请求，响应的字符集
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		// 从session中获取用户名
		HttpSession session = request.getSession();
		String name = (String) session.getAttribute("name");
		// 获取总数
		int pageSum;
		// 定义展示行数
		int pageSize = 10;
		// 获取页面输入的跳转
		String pageStr = request.getParameter("page");
		// 定义id
		String id = null;
		// 获取页面传过来的id
		id = request.getParameter("sid");
		if (id.equals("1")) {
			pageSum = QuertDaily.DailyCont(name);
			// 调用方法 计算分页参数
			List<Page> pageList = PageUtils.getPageList(pageSize, pageSum, pageStr);
			int startIndex = pageList.get(0).getStartIndex();
			// 调用查询方法
			List<Daily> dList = QuertDaily.queDailies(name, startIndex, pageSize);
			request.setAttribute("id", id);
			request.setAttribute("pageList", pageList);
			request.setAttribute("list", dList);
			request.getRequestDispatcher("mydaily.jsp").forward(request, response);
		}
		// 根据日期查寻
		if (id.equals("2")) {
			// 获取系统时间
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			Calendar c = Calendar.getInstance();
			// 过去七天
			c.setTime(new Date());
			c.add(Calendar.DATE, -7);
			Date d = c.getTime();
			String day = format.format(d);
			java.sql.Date date = java.sql.Date.valueOf(day);
			pageSum = QuertDaily.dateCont(name, date);
			// 调用方法 计算分页参数
			List<Page> pageList = PageUtils.getPageList(pageSize, pageSum, pageStr);
			int startIndex = pageList.get(0).getStartIndex();
			// 调用查询方法
			QuertDaily queryDate = new QuertDaily();
			List<Daily> dailies = queryDate.DailyDate(name, date, startIndex, pageSize);
			request.setAttribute("id", id);
			request.setAttribute("pageList", pageList);
			request.setAttribute("list", dailies);
			request.getRequestDispatcher("mydaily.jsp").forward(request, response);
		}
		if (id.equals("3")) {
			// 获取系统时间
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			Calendar c = Calendar.getInstance();
			// 过去一月
			c.setTime(new Date());
			c.add(Calendar.MONTH, -1);
			Date m = c.getTime();
			String mon = format.format(m);
			java.sql.Date date = java.sql.Date.valueOf(mon);
			pageSum = QuertDaily.dateCont(name, date);
			// 调用方法 计算分页参数
			List<Page> pageList = PageUtils.getPageList(pageSize, pageSum, pageStr);
			int startIndex = pageList.get(0).getStartIndex();
			// 调用查询方法
			QuertDaily queryDate = new QuertDaily();
			List<Daily> dailies = queryDate.DailyDate(name, date, startIndex, pageSize);
			request.setAttribute("id", id);
			request.setAttribute("pageList", pageList);
			request.setAttribute("list", dailies);
			request.getRequestDispatcher("mydaily.jsp").forward(request, response);
		}
		if (id.equals("4")) {
			// 获取系统时间
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			Calendar c = Calendar.getInstance();
			// 过去一年
			c.setTime(new Date());
			c.add(Calendar.YEAR, -1);
			Date y = c.getTime();
			String year = format.format(y);
			java.sql.Date date = java.sql.Date.valueOf(year);
			pageSum = QuertDaily.dateCont(name, date);
			// 调用方法 计算分页参数
			List<Page> pageList = PageUtils.getPageList(pageSize, pageSum, pageStr);
			int startIndex = pageList.get(0).getStartIndex();
			// 调用查询方法
			QuertDaily queryDate = new QuertDaily();
			List<Daily> dailies = queryDate.DailyDate(name, date, startIndex, pageSize);
			request.setAttribute("id", id);
			request.setAttribute("pageList", pageList);
			request.setAttribute("list", dailies);
			request.getRequestDispatcher("mydaily.jsp").forward(request, response);
		}
		if (id.equals("5")) {
			// 获取系统时间
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			Calendar c = Calendar.getInstance();
			// 过去一年
			c.setTime(new Date());
			c.add(Calendar.YEAR, -1);
			c.add(Calendar.DATE, -1);
			Date y = c.getTime();
			String year = format.format(y);
			java.sql.Date date = java.sql.Date.valueOf(year);
			pageSum = QuertDaily.dateCont(name, date);
			// 调用方法 计算分页参数
			List<Page> pageList = PageUtils.getPageList(pageSize, pageSum, pageStr);
			int startIndex = pageList.get(0).getStartIndex();
			// 调用查询方法
			QuertDaily queryDate = new QuertDaily();
			List<Daily> dailies = queryDate.DailyDate(name, date, startIndex, pageSize);
			request.setAttribute("id", id);
			request.setAttribute("pageList", pageList);
			request.setAttribute("list", dailies);
			request.getRequestDispatcher("mydaily.jsp").forward(request, response);
		}
		// 根据用户输入的关键字进行模糊查询
		if (id.equals("6")) {
			String text = request.getParameter("query");
			pageSum = QuertDaily.Dailytext(text);
			// 调用方法 计算分页参数
			List<Page> pageList = PageUtils.getPageList(pageSize, pageSum, pageStr);
			int startIndex = pageList.get(0).getStartIndex();
			// 调用查询方法
			QuertDaily queryDate = new QuertDaily();
			List<Daily> dailies = queryDate.Dailydim(text, startIndex, pageSize);
			if (dailies.size() > 0) {
				request.setAttribute("id", id);
				request.setAttribute("pageList", pageList);
				request.setAttribute("list", dailies);
				request.getRequestDispatcher("mydaily.jsp").forward(request, response);
			}
			
		}

	}
}
